# Where is this and what do we need it for?
#INCLUDE(ListHandle)

  MACRO(READ_GCC_VERSION)
     if (CMAKE_COMPILER_IS_GNUCC)
        execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                        OUTPUT_VARIABLE GCC_VERSION)
        string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
        list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
        list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)
     endif()
  ENDMACRO(READ_GCC_VERSION)


macro(MacroEmptyExternalProject proj dependencies)
 
    ExternalProject_Add(${proj}
      DOWNLOAD_COMMAND ""
      CONFIGURE_COMMAND ""
      BUILD_COMMAND ""
      INSTALL_COMMAND ""
      DEPENDS
        ${dependencies}
      )
 
endmacro()

  MACRO(FILTER_OUT FILTERS INPUTS OUTPUT)
       # Mimicks Gnu Make's $(filter-out) which removes elements 
       # from a list that match the pattern.
       # Arguments:
       #  FILTERS - list of patterns that need to be removed
       #  INPUTS  - list of inputs that will be worked on
       #  OUTPUT  - the filtered list to be returned
       # 
       # Example: 
       #  SET(MYLIST this that and the other)
       #  SET(FILTS this that)
       #
       #  FILTER_OUT("${FILTS}" "${MYLIST}" OUT)
       #  MESSAGE("OUTPUT = ${OUT}")
       #
       # The output - 
       #   OUTPUT = and;the;other
       #
       SET(FOUT "")
       FOREACH(INP ${INPUTS})
           SET(FILTERED 0)
           FOREACH(FILT ${FILTERS})
               IF(${FILTERED} EQUAL 0)
                   IF("${FILT}" STREQUAL "${INP}")
                       SET(FILTERED 1)
                   ENDIF("${FILT}" STREQUAL "${INP}")
               ENDIF(${FILTERED} EQUAL 0)
           ENDFOREACH(FILT ${FILTERS})
           IF(${FILTERED} EQUAL 0)
               SET(FOUT ${FOUT} ${INP})
           ENDIF(${FILTERED} EQUAL 0)
       ENDFOREACH(INP ${INPUTS})
       SET(${OUTPUT} ${FOUT})
   ENDMACRO(FILTER_OUT FILTERS INPUTS OUTPUT)


   MACRO(GET_HEADERS_EXTENSIONLESS DIR GLOB_PATTERN OUTPUT)
            FILE(GLOB TMP "${DIR}/${GLOB_PATTERN}" )
      #FOREACH(F ${TMP})
            #    MESSAGE(STATUS "header-->${F}<--")
      #ENDFOREACH(F ${TMP})
            FILTER_OUT("${DIR}/CVS" "${TMP}" TMP)
            FILTER_OUT("${DIR}/cvs" "${TMP}" ${OUTPUT})
            FILTER_OUT("${DIR}/.svn" "${TMP}" ${OUTPUT})
   ENDMACRO(GET_HEADERS_EXTENSIONLESS DIR GLOB_PATTERN OUTPUT)
   
MACRO(ADD_DIRS_TO_ENV_VAR _VARNAME )
 FOREACH(_ADD_PATH ${ARGN}) 
    FILE(TO_NATIVE_PATH ${_ADD_PATH} _ADD_NATIVE)
    #SET(_CURR_ENV_PATH $ENV{PATH})
    #LIST(SET _CURR_ENV_PATH ${_ADD_PATH})
    #SET(ENV{PATH} ${_CURR_ENV_PATH})${_FILE}
    IF(WIN32)
        SET(ENV{${_VARNAME}} "$ENV{${_VARNAME}};${_ADD_NATIVE}")
    ELSE(WIN32)
        SET(ENV{${_VARNAME}} "$ENV{${_VARNAME}}:${_ADD_NATIVE}")
    ENDIF(WIN32)
    #MESSAGE(" env ${_VARNAME} --->$ENV{${_VARNAME}}<---")
 ENDFOREACH(_ADD_PATH)
ENDMACRO(ADD_DIRS_TO_ENV_VAR _VARNAME )

#---------------------------------------------------
# MACRO CORRECT_PATH VAR PATH 
# corrects slashes in PATH to be cmake conformous ( / ) 
# and puts result in VAR 
#---------------------------------------------------

MACRO(CORRECT_PATH VAR PATH)
    SET(${VAR} ${PATH})
IF(WIN32)    
    STRING(REGEX REPLACE "/" "\\\\" ${VAR} "${PATH}")
ENDIF(WIN32)    
ENDMACRO(CORRECT_PATH)

MACRO(TARGET_LOCATIONS_SET_FILE FILE)
 SET(ACCUM_FILE_TARGETS ${FILE})
 FILE(WRITE ${ACCUM_FILE_TARGETS} "")
ENDMACRO(TARGET_LOCATIONS_SET_FILE FILE)

MACRO(TARGET_LOCATIONS_ACCUM TARGET_NAME)
 IF(ACCUM_FILE_TARGETS)
  IF(EXISTS ${ACCUM_FILE_TARGETS})
    GET_TARGET_PROPERTY(_FILE_LOCATION ${TARGET_NAME} LOCATION)
    FILE(APPEND ${ACCUM_FILE_TARGETS} "${_FILE_LOCATION};")
    #SET(_TARGETS_LIST ${_TARGETS_LIST} "${_FILE_LOCATION}" CACHE INTERNAL "lista dll")
    #MESSAGE("adding target -->${TARGET_NAME}<-- file -->${_FILE_LOCATION}<-- to list -->${_TARGETS_LIST}<--")
    #SET(ACCUM_FILE_TARGETS ${ACCUM_FILE_TARGETS} ${_FILE_LOCATION})
  ENDIF(EXISTS ${ACCUM_FILE_TARGETS})
 ENDIF(ACCUM_FILE_TARGETS)
ENDMACRO(TARGET_LOCATIONS_ACCUM TARGET_NAME)

MACRO(TARGET_LOCATIONS_GET_LIST _VAR)
 IF(ACCUM_FILE_TARGETS)
  IF(EXISTS ${ACCUM_FILE_TARGETS})
      FILE(READ ${ACCUM_FILE_TARGETS} ${_VAR})    
  ENDIF(EXISTS ${ACCUM_FILE_TARGETS})
 ENDIF(ACCUM_FILE_TARGETS)
ENDMACRO(TARGET_LOCATIONS_GET_LIST _VAR)


MACRO(FIND_DEPENDENCY DEPNAME INCLUDEFILE LIBRARY SEARCHPATHLIST)

MESSAGE(STATUS "searching ${DEPNAME} -->${INCLUDEFILE}<-->${LIBRARY}<-->${SEARCHPATHLIST}<--")

SET(MY_PATH_INCLUDE )
SET(MY_PATH_LIB )
SET(MY_PATH_BIN )

FOREACH( MYPATH ${SEARCHPATHLIST} )
    SET(MY_PATH_INCLUDE ${MY_PATH_INCLUDE} ${MYPATH}/include)
    SET(MY_PATH_LIB ${MY_PATH_LIB} ${MYPATH}/lib)
    SET(MY_PATH_BIN ${MY_PATH_BIN} ${MYPATH}/bin)
ENDFOREACH( MYPATH ${SEARCHPATHLIST} )

SET(MYLIBRARY "${LIBRARY}")
SEPARATE_ARGUMENTS(MYLIBRARY)

#MESSAGE( " include paths: -->${MY_PATH_INCLUDE}<--")

#MESSAGE( " ${DEPNAME}_INCLUDE_DIR --> ${${DEPNAME}_INCLUDE_DIR}<--")
    
FIND_PATH("${DEPNAME}_INCLUDE_DIR" ${INCLUDEFILE}
  ${MY_PATH_INCLUDE}
)
MARK_AS_ADVANCED("${DEPNAME}_INCLUDE_DIR")
#MESSAGE( " ${DEPNAME}_INCLUDE_DIR --> ${${DEPNAME}_INCLUDE_DIR}<--")

FIND_LIBRARY("${DEPNAME}_LIBRARY" 
    NAMES ${MYLIBRARY}
  PATHS ${MY_PATH_LIB}
)
IF(${DEPNAME}_LIBRARY)
    GET_FILENAME_COMPONENT(MYLIBNAME ${${DEPNAME}_LIBRARY} NAME_WE)
    GET_FILENAME_COMPONENT(MYBINPATH ${${DEPNAME}_LIBRARY} PATH)
    GET_FILENAME_COMPONENT(MYBINPATH ${MYBINPATH} PATH)
    SET(MYBINPATH "${MYBINPATH}/bin")
    IF(EXISTS ${MYBINPATH})
        SET(MYFOUND 0)
        FOREACH(MYPATH ${MY_ACCUM_BINARY_DEP})
            IF(MYPATH MATCHES ${MYBINPATH})
                SET(MYFOUND 1)
                #MESSAGE("found -->${MYPATH}<-->${MYBINPATH}<--")
            ENDIF(MYPATH MATCHES ${MYBINPATH})
        ENDFOREACH(MYPATH )
        IF(MYFOUND EQUAL 0)
            SET(MY_ACCUM_BINARY_DEP ${MY_ACCUM_BINARY_DEP} ${MYBINPATH})
        ENDIF(MYFOUND EQUAL 0)
    ENDIF(EXISTS ${MYBINPATH})
ENDIF(${DEPNAME}_LIBRARY)



##########################################################################
MARK_AS_ADVANCED("${DEPNAME}_LIBRARY")
#MESSAGE( " ${DEPNAME}_LIBRARY --> ${${DEPNAME}_LIBRARY}<--")
IF(${DEPNAME}_INCLUDE_DIR)
  IF(${DEPNAME}_LIBRARY)
    SET( ${DEPNAME}_FOUND "YES" )
    SET( ${DEPNAME}_LIBRARIES ${${DEPNAME}_LIBRARY} )
  ENDIF(${DEPNAME}_LIBRARY)
ENDIF(${DEPNAME}_INCLUDE_DIR)
ENDMACRO(FIND_DEPENDENCY DEPNAME INCLUDEFILE LIBRARY SEARCHPATHLIST)

##############################################################################
#SET(MACRO_MESSAGE_DEBUG TRUE)
MACRO(MACRO_MESSAGE MYTEXT)
    IF(MACRO_MESSAGE_DEBUG)
        MESSAGE("in file -->${CMAKE_CURRENT_LIST_FILE}<-- line -->${CMAKE_CURRENT_LIST_LINE}<-- message  ${MYTEXT}")
    ELSE(MACRO_MESSAGE_DEBUG)
        MESSAGE(STATUS "in file -->${CMAKE_CURRENT_LIST_FILE}<-- line -->${CMAKE_CURRENT_LIST_LINE}<-- message  ${MYTEXT}")
    ENDIF(MACRO_MESSAGE_DEBUG)
ENDMACRO(MACRO_MESSAGE MYTEXT)

MACRO(CREATE_LINK_LINES_FOR_TARGETS OUTVAR)
    FOREACH(varname ${ARGN})
        IF(${varname}_RELEASE)
           IF(${varname}_DEBUG)
               Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}_RELEASE}" debug "${${varname}_DEBUG}")
           ELSE(${varname}_DEBUG)
               Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}_RELEASE}" debug "${${varname}_RELEASE}" )
           ENDIF(${varname}_DEBUG)
        ELSE(${varname}_RELEASE)
           IF(${varname}_DEBUG)
               Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}}" debug "${${varname}_DEBUG}")
           ELSE(${varname}_DEBUG)
               Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}}" debug "${${varname}}" )
           ENDIF(${varname}_DEBUG)
        ENDIF(${varname}_RELEASE)
    ENDFOREACH(varname)
ENDMACRO(CREATE_LINK_LINES_FOR_TARGETS)


############################################################################
# Try to ascertain the version...
MACRO(FIND_OSG_VERSION)
    IF(OSG_INCLUDE_DIR)

        SET(_osg_Version_file "${OSG_INCLUDE_DIR}/osg/Version")
        IF("${OSG_INCLUDE_DIR}" MATCHES "\\.framework$" AND NOT EXISTS "${_osg_Version_file}")
            SET(_osg_Version_file "${OSG_INCLUDE_DIR}/Headers/Version")
        ENDIF()

        IF(EXISTS "${_osg_Version_file}")
          FILE(READ "${_osg_Version_file}" _osg_Version_contents)
        ELSE()
          SET(_osg_Version_contents "unknown")
        ENDIF()

        STRING(REGEX MATCH ".*#define OSG_VERSION_MAJOR[ \t]+[0-9]+.*"
            _osg_old_defines "${_osg_Version_contents}")
        STRING(REGEX MATCH ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+[0-9]+.*"
            _osg_new_defines "${_osg_Version_contents}")
        IF(_osg_old_defines)
            STRING(REGEX REPLACE ".*#define OSG_VERSION_MAJOR[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
            STRING(REGEX REPLACE ".*#define OSG_VERSION_MINOR[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
            STRING(REGEX REPLACE ".*#define OSG_VERSION_PATCH[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
        ELSEIF(_osg_new_defines)
            STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
            STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_MINOR_VERSION[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
            STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_PATCH_VERSION[ \t]+([0-9]+).*"
                "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
        ELSE()
            MESSAGE("[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
                "Failed to parse version number, please report this as a bug")
        ENDIF()

        SET(OSG_VERSION_MAJOR ${_osg_VERSION_MAJOR})
        SET(OSG_VERSION_MINOR ${_osg_VERSION_MINOR})
        SET(OSG_VERSION_PATCH ${_osg_VERSION_PATCH})

        SET(OPENSCENEGRAPH_VERSION "${_osg_VERSION_MAJOR}.${_osg_VERSION_MINOR}.${_osg_VERSION_PATCH}"
                                    CACHE INTERNAL "The version of OSG which was detected")
    ENDIF()
ENDMACRO(FIND_OSG_VERSION)

##########################################################################
MACRO(BLUEMOON_ADD_LIBRARY_PROPERTIES LIB_NAME EXPORT_SYMBOL)
   if (MSVC_IDE)
      SET_TARGET_PROPERTIES(${LIB_NAME}
                     PROPERTIES DEFINE_SYMBOL ${EXPORT_SYMBOL})
   endif (MSVC_IDE)
ENDMACRO(BLUEMOON_ADD_LIBRARY_PROPERTIES)


MACRO(LINK_WITH_DEPLIB TRGTNAME)
    FOREACH(varname ${ARGN})
      IF(varname)
        IF(${varname}_DEBUG)
          SET(DEBUG_LIBRARY ${varname}_DEBUG)
        ELSE(${varname}_DEBUG)
          SET(DEBUG_LIBRARY ${varname})
        ENDIF(${varname}_DEBUG)

        IF(${varname}_RELEASE)
          SET(RELEASE_LIBRARY ${varname}_RELEASE)
        ELSE(${varname}_RELEASE)
          SET(RELEASE_LIBRARY ${varname})
        ENDIF(${varname}_RELEASE)

        TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${${RELEASE_LIBRARY}}" debug "${${DEBUG_LIBRARY}}")
      ENDIF(varname)
    ENDFOREACH(varname)
ENDMACRO(LINK_WITH_DEPLIB)

#######################################################################
MACRO(LINK_WITH_INTERNAL TRGTNAME)
  IF(CMAKE_CONFIGURATION_TYPES)
    FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
      # prepare the postfix
      STRING(TOUPPER "${CONF}" CONF)
      SET( POSTFIX "${CMAKE_${CONF}_POSTFIX}" )

      IF(NOT CMAKE24)
        TARGET_LINK_LIBRARIES(${TRGTNAME} ${ARGN})
      ELSE(NOT CMAKE24)
        FOREACH(LINKLIB ${ARGN})
          IF(MSVC)
            TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${OUTPUT_LIBDIR}/${LINKLIB}${CMAKE_RELEASE_POSTFIX}.lib" debug "${OUTPUT_LIBDIR}/${LINKLIB}${POSTFIX}.lib")
            ADD_DEPENDENCIES(${TAGTNAME} ${LIBNKLIB})
          ELSE(MSVC)
            TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${LINKLIB}${CMAKE_RELEASE_POSTFIX}" debug "${LINKLIB}${POSTFIX}")
          ENDIF(MSVC)
        ENDFOREACH(LINKLIB ${ARGN})
      ENDIF(NOT CMAKE24)
    ENDFOREACH()
  ELSE(CMAKE_CONFIGURATION_TYPES)
    IF(NOT CMAKE24)
      TARGET_LINK_LIBRARIES(${TRGTNAME} ${ARGN})
    ELSE(NOT CMAKE24)
      FOREACH(LINKLIB ${ARGN})
        MESSAGE("${LINKLIB}")
        IF(MSVC)
          TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${OUTPUT_LIBDIR}/${LINKLIB}${CMAKE_RELEASE_POSTFIX}.lib" debug "${OUTPUT_LIBDIR}/${LINKLIB}${POSTFIX}.lib")
          ADD_DEPENDENCIES(${TAGTNAME} ${LIBNKLIB})
        ELSE(MSVC)
          MESSAGE("${LINKLIB}${CMAKE_RELEASE_POSTFIX}")
          TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${LINKLIB}${CMAKE_RELEASE_POSTFIX}" debug "${LINKLIB}${POSTFIX}")
        ENDIF(MSVC)
      ENDFOREACH(LINKLIB ${ARGN})
    ENDIF(NOT CMAKE24)

  ENDIF(CMAKE_CONFIGURATION_TYPES)
ENDMACRO(LINK_WITH_INTERNAL)

####################################################################
MACRO(SETUP_LINK_LIBRARIES)
  SET(TARGET_LIBRARIES ${TARGET_COMMON_LIBRARIES})

  #FOREACH(LINKLIB ${TARGET_ADDED_LIBRARIES})
  #  SET(TO_INSERT TRUE)
  #  FOREACH(value ${TARGET_COMMON_LIBRARIES})
  #    IF(${value} STREQUAL ${LINKLIB})
  #      SET(TO_NSERT FALSE)
  #    ENDIF(${value} STREQUAL ${LINKLIB})
  #  ENDFOREACH(value ${TARGET_COMMON_LIBRARIES})
  #  IF(TO_INSERT)
  #    LIST(APPEND TARGET_LIBRARIES ${LINKLIB})
  #  ENDIF(TO_INSERT)
  #ENDFOREACH(LINKLIB)

  SET(ALL_GL_LIBRARIES ${OPENGL_gl_LIBRARY})

  IF( LIB_INTERNALS )
    LINK_WITH_INTERNAL(${TARGET_TARGETNAME} ${LIB_INTERNALS})
  ENDIF(LIB_INTERNALS)

  IF( TARGET_EXTERNAL_LIBRARIES )
    TARGET_LINK_LIBRARIES(${TARGET_TARGETNAME} ${TARGET_EXTERNAL_LIBRARIES})
  ENDIF(TARGET_EXTERNAL_LIBRARIES)

  IF(TARGET_COMMON_LIBRARIES)
    LINK_WITH_DEPLIB(${TARGET_TARGETNAME} ${TARGET_COMMON_LIBRARIES})
  ENDIF(TARGET_COMMON_LIBRARIES)
  IF(LIB_DEPS)
    LINK_WITH_DEPLIB(${TARGET_TARGETNAME} ${LIB_DEPS})
  ENDIF(LIB_DEPS)

  IF(MSVC)
    LINK_WITH_DEPLIB(${TARGET_TARGETNAME} ${ALL_GL_LIBRARIES})
  ENDIF(MSVC)

ENDMACRO(SETUP_LINK_LIBRARIES)

##########################################################################
MACRO(SET_LABEL_AND_GROUP FOLDERNAME)
  SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES FOLDER ${FOLDERNAME})
  IF( TARGET_LABEL )
    SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES PROJECT_LABEL "${TARGET_LABEL}${TARGET_TARGETNAME}")
  ENDIF(TARGET_LABEL)
ENDMACRO(SET_LABEL_AND_GROUP)


########################################################################
MACRO(BLUEMOON_ADD_LIBRARY LIB_NAME)
  IF( NOT TARGET_NAME )
    SET( TARGET_NAME ${LIB_NAME} )
  ENDIF( NOT TARGET_NAME )

  IF(NOT TARGET_TARGETNAME)
    SET(TARGET_TARGETNAME ${LIB_NAME})
  ENDIF(NOT TARGET_TARGETNAME)

  SET(LIBRARY_TYPE ${ARGV2})

  if (NOT LIBRARY_TYPE OR "${LIBRARY_TYPE}" STREQUAL "FRAMEWORK")
     SET(LIBRARY_TYPE SHARED)
     SET(BLUEMOON_ALLOW_FRAMEWORK ON)
  else()
     SET(BLUEMOON_ALLOW_FRAMEWORK OFF)
  endif()

  # check unicode set
  IF( BUILD_AT_UNICODE )
    ADD_DEFINITIONS(-DUNICODE)
  ENDIF( BUILD_AT_UNICODE )

  ADD_LIBRARY(${TARGET_TARGETNAME} ${LIBRARY_TYPE}
       ${LIB_PUBLIC_HEADERS}
       ${LIB_SOURCES}
  )

  #LINK_WITH_INTERNAL(${LIB_NAME} ${LIB_INTERNALS})
  #LINK_WITH_DEPLIB(${LIB_NAME} ${LIB_DEPS})

  SETUP_LINK_LIBRARIES()

  SET_LABEL_AND_GROUP("BMCore")


  INCLUDE(ModuleInstall OPTIONAL)

ENDMACRO(BLUEMOON_ADD_LIBRARY)

###########################################################################
MACRO (BLUEMOON_ADD_PLUGIN LIB_NAME)
   IF(NOT TARGET_TARGETNAME)
     SET(TARGET_TARGETNAME "${TARGET_DEFAULT_PREFIX}${LIB_NAME}")
   ENDIF(NOT TARGET_TARGETNAME)

   IF(NOT TARGET_NAME)
     SET(TARGET_NAME ${LIB_NAME})
   ENDIF(NOT TARGET_NAME)

   #put the binary into a "STAGE plugins" folder
   IF (WIN32)
      # This design only makes sense on windows because there is no bin dir for libraries.
      SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin/${BLUEMOON_PLUGINS_DIR})
      SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib/${BLUEMOON_PLUGINS_DIR})
    ELSE(WIN32)
     SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib/${BLUEMOON_PLUGINS_DIR})
   ENDIF(WIN32)

   SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${OUTPUT_LIBDIR})
   SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})

   IF(WIN32)
     SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})
   ELSE(WIN32)
     SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR}) 
   ENDIF(WIN32)
# For each configuration (Debug, Release, MinSizeRel... and/or anything the
# user chooses)
   FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
     SET(OLDCONF ${CONF})
     # Go uppercase (DEBUG, RELEASE...)
     STRING(TOUPPER "${CONF}" CONF)


 #Handle the debug differently by putting the output into a debug subfolder
       IF (${CONF} STREQUAL "DEBUG")
         SET(DEBUG_FOLDER "Debug")
       ELSE (${CONF} STREQUAL "DEBUG")
         SET(DEBUG_FOLDER "")
       ENDIF (${CONF} STREQUAL "DEBUG")

       SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
       SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}/${DEBUG_FOLDER}")
       IF(WIN32)
         SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}/${DEBUG_FOLDER}")
       ELSE()
         SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
       ENDIF()
   ENDFOREACH()

  # check unicode set
  IF( BUILD_AT_UNICODE )
    ADD_DEFINITIONS(-DUNICODE)
  ENDIF( BUILD_AT_UNICODE )

   ADD_LIBRARY(${TARGET_TARGETNAME} MODULE
       ${LIB_PUBLIC_HEADERS}
       ${LIB_SOURCES}
   )
   SETUP_LINK_LIBRARIES()
   SET_LABEL_AND_GROUP("BMPlugin")

   SET(INSTALL_INCDIR include)
   SET(INSTALL_BINDIR bin)
   IF(WIN32)
     SET(INSTALL_LIBDIR bin/${BLUEMOON_PLUGINS_DIR})
     SET(INSTALL_ARCHIVEDIR lib/${BLUEMOON_PLUGINS_DIR})
   ELSE(WIN32)
     SET(INSTALL_LIBDIR lib/${BLUEMOON_PLUGINS_DRI})
     SET(INSTALL_ARCHIVEDIR lib/${BLUEMOON_PLUGINS_DIR})
       SET(INSTALL_FRAMEWORKDIR Frameworks)
   ENDIF(WIN32)

   SET(HEADERS_GROUP "Header Files")

   SOURCE_GROUP(
       ${HEADERS_GROUP}
       FILES ${LIB_PUBLIC_HEADERS}
   )

 #IF(MSVC AND OSG_MSVC_VERSIONED_DLL)
 #     HANDLE_MSVC_DLL()
 #  ENDIF(MSVC AND OSG_MSVC_VERSIONED_DLL)

   if (APPLE)
      set(CMAKE_INSTALL_RPATH "${BLUEMOON_FRAMEWORK_INSTALL_NAME_DIR}")
      
      if (BLUEMOON_DYLIB_INSTALL_NAME_DIR)
         SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
            INSTALL_NAME_DIR "${BLUEMOON_DYLIB_INSTALL_NAME_DIR}"
         )
      else()
         set(CMAKE_INSTALL_RPATH "${INSTALL_INCDIR}/${LIB_NAME}")
         SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
            INSTALL_NAME_DIR "${INSTALL_INCDIR}/${LIB_NAME}"
         )
      endif()
   endif()
   
  INSTALL (
     FILES        ${LIB_PUBLIC_HEADERS}
     DESTINATION  ${INSTALL_INCDIR}/${LIB_NAME}
     COMPONENT BLUEMOON-dev
   )
   if (MSVC)
      INSTALL(
        TARGETS ${TARGET_TARGETNAME}
        CONFIGURATIONS Release RelWithDebInfo MinSizeRel
        RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT BLUEMOON
        LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT BLUEMOON
        FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT BLUEMOON
        ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT BLUEMOON-dev
      )
      INSTALL(
        TARGETS ${TARGET_TARGETNAME}
        CONFIGURATIONS Debug
        RUNTIME DESTINATION ${INSTALL_BINDIR}/ COMPONENT BLUEMOON
        LIBRARY DESTINATION ${INSTALL_LIBDIR}/ COMPONENT BLUEMOON
        FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT BLUEMOON
        ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT BLUEMOON-dev
      )
   else()
      INSTALL(
        TARGETS ${TARGET_TARGETNAME}
        RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT BLUEMOON
        LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT BLUEMOON
        FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT BLUEMOON
        ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT BLUEMOON-dev
      )
   endif()
   
   
   
   #make sure that the debug build is the only one that gets built in the debug folder.
   IF (MSVC)
     SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES DEBUG_OUTPUT_NAME ${LIB_NAME})
   ENDIF (MSVC)

ENDMACRO(BLUEMOON_ADD_PLUGIN)


################################################################################
MACRO(BLUEMOON_ADD_EXE EXE_TARGET_NAME IS_COMMANDLINE_APP)

  IF( NOT TARGET_NAME )
    SET(TARGET_NAME ${EXE_TARGET_NAME})
  ENDIF( NOT TARGET_NAME )
  IF(NOT TARGET_TARGETNAME)
    SET(TARGET_TARGETNAME "${TARGET_DEFAULT_PREFIX}${TARGET_NAME}")
  ENDIF(NOT TARGET_TARGETNAME)

  IF( NOT TARGET_LABEL)
    SET(TARGET_LABEL "${TARGET_DEFAULT_LABEL_PREFIX} ${TARGET_NAME}")
  ENDIF(NOT TARGET_LABEL)

  IF(${IS_COMMANDLINE_APP})
    ADD_EXECUTABLE(${TARGET_TARGETNAME} ${TARGET_SRC} ${TARGET_H})
  ELSE(${IS_COMMANDLINE_APP})

  IF(WIN32)
    IF(REQUIRE_WINMAIN_FLAG)
      SET(PLATFORM_SPECIFIC_CONTROL WIN32)
    ENDIF(REQUIRE_WINMAIN_FLAG)
  ENDIF(WIN32)

  # check unicode set
  IF( BUILD_AT_UNICODE )
    ADD_DEFINITIONS(-DUNICODE)
  ENDIF( BUILD_AT_UNICODE )

  ADD_EXECUTABLE(${TARGET_TARGETNAME} ${PLATFORM_SPECIFIC_CONTROL} ${TARGET_SRC} ${TARGET_H})

  ENDIF(${IS_COMMANDLINE_APP})

  #  SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES FOLDER "Application")

  SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES PROJECT_LABEL "${TARGET_LABEL}")
  SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES OUTPUT_NAME "${TARGET_NAME}")
  SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES DEBUG_OUTPUT_NAME "${TARGET_NAME}${CMAKE_DEBUG_POSTFIX}")
    SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES RELEASE_OUTPUT_NAME "${TARGET_NAME}${CMAKE_RELEASE_POSTFIX}")
    SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES RELWITHDEBINFO_OUTPUT_NAME "${TARGET_NAME}${CMAKE_RELWITHDEBINFO_POSTFIX}")
    SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES MINSIZEREL_OUTPUT_NAME "${TARGET_NAME}${CMAKE_MINSIZEREL_POSTFIX}")

    # IF(MSVC_IDE)
    #  SET_OUTPUT_DIR_PROPERTY_260(${TARGET_TARGETNAME} "")
    #ENDIF(MSVC_IDE)
    SETUP_LINK_LIBRARIES()

    SET_LABEL_AND_GROUP("Application")

ENDMACRO(BLUEMOON_ADD_EXE)
